import 'dart:convert';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:dio/dio.dart';

FormData getSign(Map parameter) {
  var key = '123dmqc321';
  var timestamp = new DateTime.now().millisecondsSinceEpoch;
  var versionNumber = 'V1';
  var dataType = 'JSON';
  parameter['data_type'] = dataType;
  parameter['version'] = versionNumber;
  parameter['timestamp'] = "$timestamp";
  List<String> allKeys = [];
  parameter.forEach((key, value) {
    if (!(value is MultipartFile)) {
      allKeys.add(key.toString() + value.toString());
    }
  });
  allKeys.sort((obj1, obj2) {
    return obj1.compareTo(obj2);
  });
  String pairsString = allKeys.join("");
  String sign = key + pairsString + key;
  String signString = generateMd5(sign).toUpperCase();
  parameter['sign'] = signString;
  return FormData.fromMap(new Map<String, dynamic>.from(parameter));
}

// FormData getSign(Map parameter) {
//   var key = '123dmqc321';
//   var timestamp = new DateTime.now().millisecondsSinceEpoch;
//   var versionNumber = 'V1';
//   var dataType = 'JSON';
//   parameter['data_type']=dataType;
//   parameter['version']=versionNumber;
//   parameter['timestamp']="$timestamp";
//   List<String> allKeys = [];
//   parameter.forEach((key, value) {
//     allKeys.add(key + value);
//   });
//   allKeys.sort((obj1, obj2) {
//     return obj1.compareTo(obj2);
//   });
//   String pairsString = allKeys.join("");
//   String sign = key + pairsString + key;
//   String signString = generateMd5(sign).toUpperCase();
//   parameter['sign']=signString;
//   return parameter;
// }

String generateMd5(String data) {
  var content = new Utf8Encoder().convert(data);
  var digest = md5.convert(content);
  return hex.encode(digest.bytes);
}